﻿using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;

namespace MuddyBoots.GreenlightQC.Mobile.UI.View.Triggers
{
    public class ExecuteCommandAction : TriggerAction<FrameworkElement>
    {
        public string Command { get; set; }

        public string CommandParameter { get; set; }

        protected override void Invoke(object o)
        {
            if (Command != null)
            {
                object context = AssociatedObject.DataContext;
                if (context != null)
                {
                    var cmd = context.GetType().GetProperty(Command).GetValue(context, null) as ICommand;
                    if (cmd != null && cmd.CanExecute(o))
                    {
                        cmd.Execute(o);
                    }
                }
            }
        }
    }
}
